package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.vo.AgentDiscountVo;
import com.weixing.mall.provider.model.domain.AgentDiscount;
import com.weixing.mall.provider.model.dto.AgentDiscountDto;
import com.weixing.mall.provider.service.IAgentDiscountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理商活动折扣相关Api
 *
 * @author  Small
 * @date  2019/10/17 11:09
 * @since  1.0
 **/
@RestController
@RequestMapping("agent/discount")
public class AgentDiscountController extends BaseController {

    @Autowired
    private IAgentDiscountService agentDiscountService;


    /**
     * 新增/修改
     *
     * @param dto    参数
     * @return       结果
     */
    @PostMapping("")
    @SysLog(type = OperateEnum.S, desp = "编辑代理活动折扣")
    public Result save(AgentDiscountDto dto) {
        AgentDiscount agentDiscount = new AgentDiscount();
        BeanUtils.copyProperties(dto, agentDiscount);
        agentDiscount.setRuleName(dto.getName());
        if (null == dto.getId()) {
            AgentDiscount discount = agentDiscountService.selectByName(dto.getName());
            if (null != discount){
                return ResultUtil.error("已存在同名规则");
            }
            agentDiscountService.save(agentDiscount);
        } else {
            agentDiscountService.updateById(agentDiscount);
        }
        return ResultUtil.success();
    }

    /**
     * 根据id查询规则详情
     *
     * @param id    id
     * @return      规则详情
     */
    @GetMapping("/{id}")
    public Result<AgentDiscountVo> detail(@PathVariable Long id){
        AgentDiscount agentDiscount = agentDiscountService.getById(id);
        if (null == agentDiscount){
            return ResultUtil.error("折扣规则不存在");
        }
        AgentDiscountVo vo = new AgentDiscountVo();
        BeanUtils.copyProperties(agentDiscount,vo);
        return ResultUtil.success(vo);
    }


    /**
     * 根据名称查询规则
     *
     * @param name  规则名称
     * @return      规则列表
    */
    @GetMapping("/getByName")
    public Result<List<AgentDiscountVo>> getByName(String name){
        List<AgentDiscountVo> list = agentDiscountService.getByName(name);
        return ResultUtil.success(list);
    }


    /**
     * 删除折扣规则
     *
     * @param id   id
     * @return     结果
     */
    @GetMapping("del/{id}")
    @SysLog(type = OperateEnum.D, desp = "删除代理商活动折扣")
    public Result del(@PathVariable Long id){
        boolean b = agentDiscountService.removeById(id);
        return b ? ResultUtil.success():ResultUtil.error();
    }

    /**
     * 代理商活动折扣规则列表
     *
     * @param name    规则名称
     * @return        列表
     */
    @GetMapping("")
    public Result<IPage<AgentDiscountVo>> list(String name){
        IPage<AgentDiscountVo> list = agentDiscountService.listPage(initMpPage(),name);
        return ResultUtil.success(list);
    }


}
